package cn.cboard.gateway;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Collection;

/**
 * @author 
 * 
 * Context object to hold returned page + row ids + page #
 *
 */
public class PageContext implements Serializable {

	protected Object page; // current page
	protected Object firstEntry; //  first row id 
	protected Object lastEntry; // last row id

	protected int pageNumber; // page number
	protected int totalNumberOfPages; //  total number of pages for query
	protected int action; // paging action
	protected PageContext prevContext; // previous context state

	public PageContext(PageContext that) {
		this.page = that.page;
		this.firstEntry = that.firstEntry;
		this.lastEntry = that.firstEntry;
		this.pageNumber = that.pageNumber;
		this.totalNumberOfPages = that.totalNumberOfPages;
		this.action = that.action;
	}

	public PageContext() {
		pageNumber = 0;
		totalNumberOfPages = -1;
	}

	/**
	 * Returns the firstEntry.
	 * @return Object
	 */
	public Object getFirstEntry() {
		return firstEntry;
	}

	/**
	 * Returns the lastEntry.
	 * @return Object
	 */
	public Object getLastEntry() {
		return lastEntry;
	}

	/**
	 * Returns the page.
	 * @return Object[]
	 */
	public Object[] getPage() {
		return ((Collection) page).toArray();
	}

	/**
	 * Returns the page and converts to the specified class array
	 * @return as (klass[])
	 */
	public Object[] getPage(Class klass) {
		Object[] array = (Object[]) Array.newInstance(klass, 0);
		return ((Collection) page).toArray(array);
	}

	/**
	 * Sets the firstEntry.
	 * @param firstEntry The firstEntry to set
	 */
	public void setFirstEntry(Object firstEntry) {
		this.firstEntry = firstEntry;
	}

	/**
	 * Sets the lastEntry.
	 * @param lastEntry The lastEntry to set
	 */
	public void setLastEntry(Object lastEntry) {
		this.lastEntry = lastEntry;
	}

	/**
	 * Sets the page.
	 * @param page The page to set
	 */
	public void setPage(Object page) {
		this.page = page;
	}

	/**
	 * Returns the pageNumber.
	 * @return int
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * Returns the totalNumberOfPages.
	 * @return int
	 */
	public int getTotalNumberOfPages() {
		return totalNumberOfPages;
	}

	/**
	 * Sets the pageNumber.
	 * @param pageNumber The pageNumber to set
	 */
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	/**
	 * Sets the totalNumberOfPages.
	 * @param totalNumberOfPages The totalNumberOfPages to set
	 */
	public void setTotalNumberOfPages(int totalNumberOfPages) {
		this.totalNumberOfPages = totalNumberOfPages;
	}

	public boolean isFirstPage() {
		return (pageNumber == 1);
	}

	public boolean isLastPage() {
		return (pageNumber == totalNumberOfPages);
	}

	/**
	 * Returns the action.
	 * @return int
	 */
	public int getAction() {
		return action;
	}

	/**
	 * Returns the prevContext.
	 * @return PageContext
	 */
	public PageContext getPrevContext() {
		return prevContext;
	}

	/**
	 * Sets the prevContext.
	 * @param prevContext The prevContext to set
	 */
	public void setPrevContext(PageContext prevContext) {
		this.prevContext = prevContext;
	}

	/**
	 * Sets the action.
	 * @param action The action to set
	 */
	public void setAction(int action) {
		this.action = action;
	}

}
